package com.gxa.admin.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.gxa.admin.mapper.OrderListMapper;
import com.gxa.admin.pojo.dto.OrderListDto;
import com.gxa.admin.pojo.vo.OrderListVo;
import com.gxa.admin.service.OrderListService;
import com.gxa.admin.utils.QueryResult;
import com.gxa.core.result.Result;
import com.gxa.service.OrderListRpcDto;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @Author：lizhijun
 * @Package：{PROJECT_NAME}
 * @name：{DATE} {NAME}
 */
@Service
public class OrderListServiceImpl implements OrderListService {
    @Resource
    private OrderListMapper orderListMapper;


    @Override
    public QueryResult getOrderList(OrderListDto orderListDto) {
        orderListDto.setLimit(5);
        //分页查询
        Page<OrderListVo> page= PageHelper.startPage(orderListDto.getPage(),orderListDto.getLimit());
        //查询订单列表
        List<OrderListVo> orderList = this.orderListMapper.findAllOderList(orderListDto);
        QueryResult result = new QueryResult();
        result.setData(orderList);
        result.setCount(page.getTotal());
        return result;
    }

    @Override
    public Result acceptOrder(OrderListDto orderListDto) {
        if(orderListDto.getStatus().equals("已完成")){
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date currentDate = new Date();
            String currentTimestamp = sdf.format(currentDate);
            orderListDto.setArrived(currentTimestamp);

        }
       Integer result = orderListMapper.updateOrderStatus(orderListDto);
       Result result2 = new Result();
        if (result==1){
            result2.setMsg("更改成功");
        }else {
            result2.setMsg("更改失败");
        }
        return  result2;
    }

    @Override
    public int addOrderList(OrderListRpcDto orderListRpcDto) {

        return orderListMapper.addOrderList(orderListRpcDto);
    }


}
